package org.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class App
{
    public static void main(String[] args) throws IOException {
        Document document = Jsoup.parse(new URL("https://www.dygangs.net/"),5000);
        Elements elements = document.select(".bg-fleet a");
        elements.remove(0);
        elements.remove(elements.size()-1);
        elements.remove(elements.size()-1);
        List<Map<String,Object>> types = new ArrayList<>();
        for (Element element : elements) {
            List<Map<String,Object>> moves = new ArrayList<>();
            String type = element.text();
            String href = element.attr("href");
            System.out.println("href = " + href);
            System.out.println("type = " + type);
            int count = 0;
            for (int i = 1;i<=1;i++){
                if(i==1){
                    document = Jsoup.parse(new URL("https://www.dygangs.net" + href),5000);
                }else{
                    document = Jsoup.parse(new URL("https://www.dygangs.net" + href +"index_" + i + ".htm" ),5000);
                }
                Elements elements1 = document.select(".border1 a");
                for (Element element1 : elements1) {
                    String href1 = element1.attr("href");
                    document = Jsoup.parse(new URL("https://www.dygangs.net" + href1),5000);
                    Element element2 = document.selectFirst("#dede_content>p");
                    String image = element2.select("img").attr("src");

                    String desc = element2.text();
                    if("".equals(desc)||" ".equals(desc)){
                        desc =  document.select("#dede_content>p").get(1).text();
                        if("".equals(desc)||" ".equals(desc)){
                            desc =  document.select("#dede_content>p").get(2).text();
                        }
                    }
                    Map<String,String> map = new HashMap<>();
                    String[] strings = desc.split("◎");
                    for (String string : strings) {
                        if(string.indexOf("片　　名")>-1){
                            map.put("片　　名",string.substring("片　　名".length()+1));
                        } else if(string.indexOf("产　　地")>-1){
                            map.put("产　　地",string.substring("产　　地".length()+1));
                        }else if(string.indexOf("类　　别")>-1){
                            map.put("类　　别",string.substring("类　　别".length()+1));
                        }else if(string.indexOf("语　　言")>-1){
                            map.put("语　　言",string.substring("语　　言".length()+1));
                        }else if(string.indexOf("上映日期")>-1){
                            map.put("上映日期",string.substring("上映日期".length()+1));
                        }else if(string.indexOf("IMDb评分")>-1){
                            map.put("IMDb评分",string.substring("IMDb评分".length()+1));
                        }else if(string.indexOf("豆瓣评分")>-1){
                            map.put("豆瓣评分",string.substring("豆瓣评分".length()+1));
                        }else if(string.indexOf("片　　长")>-1){
                            map.put("片　　长",string.substring("片　　长".length()+1));
                        }else if(string.indexOf("导　　演")>-1){
                            map.put("导　　演",string.substring("导　　演".length()+1));
                        }else if(string.indexOf("编　　剧")>-1){
                            map.put("编　　剧",string.substring("编　　剧".length()+1));
                        }else if(string.indexOf("主　　演")>-1){
                            map.put("主　　演",string.substring("主　　演".length()+1));
                        }
                    }


                    count++;
                    Map<String,Object> move = new HashMap<>();
                    System.out.println("====================================");
                    System.out.println("count = " + count);
                    move.put("desc",map);
                    move.put("img",image);
                    moves.add(move);
//                    System.out.println("url= " + "https://www.dygangs.net" + href1);
//                    System.out.println("image = " + image);
//                    String[] strings = desc.split(",");
//                    System.out.println("desc = " + Arrays.toString(strings));

                }
                Map<String,Object> m = new HashMap<>();
                m.put(type, moves);
                m.put("name", type);
                types.add(m);
            }


        }
        for (Map<String, Object> type : types) {
            System.out.println("type = " + type);
        }
//        System.out.println("types = " + types);
    }

    public static void main2(String[] args) throws IOException {
        Document document = Jsoup.parse(new URL("https://www.jiushujie.com/"), 5000);
        Elements as = document.select(".book_list li>a");
        for (Element a : as) {
            String href = a.attr("href");
            href = "https://www.jiushujie.com" + href;
            System.out.println("href = " + href);
            document = Jsoup.parse(new URL(href), 5000);

            String name = document.selectFirst(".info>h2").text();
            String pic = document.selectFirst(".book_pic>img").attr("src");
            String detail = document.selectFirst(".info>.detail").text();
            String[] strings = detail.split("[\\s/]+");
            Element Desc = document.selectFirst(".book_introduction_container");
            String desc = "";
            if(Desc!=null){
                desc = Desc.text();
            }


            Map<String,Object> book = new HashMap<String,Object>();
            book.put("name",name);
            book.put("pic",pic);
            book.put("detail", Arrays.toString(strings));
            book.put("desc",desc);

            System.out.println("book = " + book);

        }
    }


    public static void main1( String[] args ) throws IOException {
        String path = "src/main/resources/mybatis.xml";
        Document document = Jsoup.parse(new File(path));
        Element environments = document.select("environments").get(0);
        String Default = environments.attr("default");
        System.out.println(Default);

        Elements environment = environments.select("environment");

        for (Element element : environment) {
            if(element.attr("id").equals(Default)){
                Elements property = element.select("property");
                for (Element element2:property){
                    String value = String.format("name=%s,value=%s",element2.attr("name"),element2.attr("value"));
                    System.out.println(value);
                }

            }
        }

        Elements mappers = document.select("mapper");
        for (Element element : mappers){
            if(!"".equals(element.attr("resource"))){
                System.out.println("resource+"+element.attr("resource"));
            }
        }

    }
}
